package com.xiaokan.test;

import java.util.Random;

import junit.framework.TestCase;

import com.xiaokan.datastructures.Heap;
import com.xiaokan.pojo.Tuple;
import com.xiaokan.preparation.MergeSort;
import com.xiaokan.utils.SortThread;

public class MergeSortTestCases extends TestCase {

	public void testByteArrayAndIntConversion() throws Exception {
		MergeSort s = new MergeSort(10);
		byte[] b = s.int2bytearray(124755);
		for (Byte i : b) {
			System.out.println(i + " ");
		}
		System.out.println(s.bytearray2int(b));
	}

	public void testW() throws Exception {
		MergeSort s = new MergeSort(10);
		byte[] b = s.int2bytearray(124755);
		for (Byte i : b) {
			System.out.println(i + " ");
		}
		System.out.println(s.bytearray2int(b));
	}

	public void testSortThread() throws Exception {
		MergeSort s = new MergeSort(8);
		int[] num = new int[8000000];
		SortThread threads[] = new SortThread[8];
		Random rand = new Random();
		for (int i = 0; i < num.length; i++) {
			num[i] = rand.nextInt(200);
		}
		for (int i = 0; i < 8; i++) {
			threads[i] = new SortThread(num, i * 1000000, (i + 1) * 1000000);
		}
		s.startAll(threads);
		for (int i = 0; i < 8; i++) {
			for (int j = 1; j < 1000000; j++) {
				if (num[i * 1000000 + j - 1] > num[i * 1000000 + j])
					System.out.println("Something is wrong");
			}
			System.out.println();
		}

	}

	public void testHeap() throws Exception {
		Heap<Tuple> heap = new Heap<Tuple>(100);

		for (int i = 0; i < 100; i++) {
			Tuple t = new Tuple("", 100 - i, 0);
			heap.add(t, i);
		}
		Object d[] = heap.getData();
		int o[] = heap.getOrg();
		for (int i = 0; i < 100; i++) {
			Tuple t = (Tuple) d[i];
			assert(t.getDocId()+o[i] == 100);
		}

		while (heap.getTop() != null) {
			Tuple t = heap.getTop();
			int org = heap.pop();
			System.out.format("%d %d\n", t.getDocId(), org);
		}
	}

}
